<?php


namespace App\Http\Controllers\Api;


use App\Dengje\Utils\Settings;
use App\Http\Controllers\Api\Repositories\PostsRepositores;
use App\Http\Controllers\Api\Repositories\WxCircleRepositores;
use App\Http\Controllers\Api\Repositories\WxUserRepositores;
use App\Http\Requests\Api\PostsRequests\CircleRequest;
use App\Lib\JwtAuth;
use App\Models\Posts\WxPost;
use App\Models\WxCircle;
use App\Models\WxClause;
use App\Models\WxSearch;
use App\Models\WxSetting;
use App\Models\WxUser;
use Illuminate\Http\Request;

class CommonController extends BaseController
{
    /**
     * 搜索
     * @param Request $request
     * @param type 0:帖子；1：圈子；2：用户
     */
    public function search(Request $request)
    {
        $keyword = $request->keyword;
        $type = $request->type;
        $limit = $request->input('limit', 10);
        $token = $request->header('token');
        $uid = 0;
        if ($token) {
            if (JwtAuth::verifyToken($token)) {
                $uid = JwtAuth::getUserId($token, 'uid');
            }
        }
        if ($type == 0) {
            $data = PostsRepositores::searchPosts($keyword, $uid, $limit);
        } elseif ($type == 1) {
            $data = WxCircleRepositores::searchCircle($keyword);
        } else {
            $data = WxUserRepositores::searchUser($keyword, $uid, $limit);
        }
        return $this->success($data);

    }

    /**
     * 统计搜索结果数量
     */
    public function searcheCountJson(Request $request)
    {
        $keyword = $request->keyword;
        $posts_count = WxPost::where('is_examine', 1)->where('posts_state', 0)->where('posts_content', 'like', '%' . $keyword . '%')->count();
        $circle_count = WxCircle::where('circle_state', 1)
            ->where(function ($query) use ($keyword) {
                $query->orWhere('circle_name', 'like', '%' . $keyword . '%')
                    ->orWhere('circle_introduce', 'like', '%' . $keyword . '%');
            })
            ->count();

        $user_count = WxUser::where(function ($q) use ($keyword) {
            $q->where('user_name', 'like', '%' . $keyword . '%');
        })->count();
        return $this->success(compact('posts_count', 'circle_count', 'user_count'));
    }

    /**
     * 热门搜索列表
     */
    public function hotSearch()
    {
        return $this->success(WxSearch::where('is_hot', 1)->get(['search_content']));
    }

    /**
     * 首页轮播搜索关键字列表
     */
    public function HomeCarousel()
    {
        return $this->success(WxSearch::where('is_home_carousel', 1)->get(['search_content']));
    }


    /**
     * 获取条款内容
     */
    public function getClauseDetail(Request $request)
    {
        $id = $request->id;
        $data = WxClause::where('id', $id)->first(['content']);
        return $this->success($data);
    }

    /**
     * 获取配置项
     * @param Request $request
     */
    public function configData(Request $request)
    {

        $members_popup_poster = Settings::get('members_popup_poster');
        $members_poster = Settings::get('members_poster');
        $user_background_maps = Settings::get('user_background_maps');
        $official_popup_poster = Settings::get('official_popup_poster');
        $authentication_popup_poster = Settings::get('authentication_popup_poster');
        $app_login_bg = Settings::get('app_login_bg');
        $app_title = Settings::get('app_title');
        $app_intro = Settings::get('app_intro');
        $about_copyright = Settings::get('about_copyright');
        $about_logo = Settings::get('about_logo');
        $about_title = Settings::get('about_title');
        $data = compact('about_title', 'about_logo', 'about_copyright', 'app_title', 'app_intro', 'app_login_bg', 'members_popup_poster', 'members_poster', 'user_background_maps', 'official_popup_poster', 'authentication_popup_poster');
        return $this->success($data);
    }

    /**
     * 用户搜索列表
     */
    public function mySearch(Request $request)
    {
        $token = $request->header('token');
        $uid = '';
        if ($token) {
            $uid = JwtAuth::getUserId($token, 'uid');
            return $this->success(WxSearch::where('user_id', $uid)
                ->orderBy('id', 'desc')
                ->get(['search_content', 'id']));
        }else{
            return $this->success([]);
        }
    }

    /**
     * 用户删除搜索记录
     * id
     */
    public function myDelSearch(Request $request)
    {
        $id = $request->id;
        return $this->success(WxSearch::where('id', $id)->delete());
    }

    /**
     * 用户删除全部搜索记录
     * id
     */
    public function myDelAllSearch(Request $request)
    {
        $uid = $request->uid;
        return $this->success(WxSearch::where('user_id', $uid)->delete());
    }


}
